<?php
/**
 * collection of static methods useful to build html widgets
 * 
 * Most of the methods will accept as an argument an array of parameters like
 * id, class, script, name, value and so on. 
 */ 
class Html
{
  static public function mkSelect($aParams)
   {
    if(isset($aParams['id']))
    {
      $id = ' id="'.$aParams['id'].'"';
    }
    else
    {
      $id = '';
    }
    
    if(isset($aParams['class']))
    {
      $class = ' class="'.$aParams['class'].'"';
    }
    else
    {
      $class = '';
    }
    
    if(isset($aParams['name']))
    {
      $name = ' name="'.$aParams['name'].'"';
    }
    else
    {
      $name = '';
    }
    
    if(isset($aParams['script']))
    {
      $script = ' '.$aParams['script'];
    }
    else
    {
      $script = '';
    }
    
    $html = EOL.'<select'.$name.$id.$class.$script.'>';
    
    if(isset($aParams['values']))
    {
      foreach($aParams['values'] as $key=>$value)
      {
        if(isset($aParams['selected']) and $key ==  $aParams['selected'])
        {
          $selected = ' selected="selected"';
        }
        else
        {
          $selected = '';
        }
        
        if(isset($aParams['labelLength']))
        {
          $max=$aParams['labelLength']; 
          if(strlen($value)>=$max){$value=substr($value,0,$max); 
          $espace=strrpos($value," "); 
          $value=substr($value,0,$espace)." ..."; }  
        }
        
        $html.= EOL.' <option'.$selected.' value="'.$key.'">'.$value.'</option>';
      }
    }
    $html.= EOL.'</select>';
    
    return $html;
   }
  static public function mkLabel($aParams)
  {
    if(isset($aParams['id'])){$id = ' id="'.$aParams['id'].'"';}
    else{$id = '';}
    if(isset($aParams['script'])){$script = ' '.$aParams['script'];}
    else{$script = '';}
    if(isset($aParams['class'])){$class = ' class="'.$aParams['class'].'"';}
    else{$class = '';}
    if(isset($aParams['for'])){$for=' for="'.$aParams['for'].'"';}
    else{$for='';}
    $html = '<label'.$for.$script.$class.$id.'>'.$aParams['text'].'</label>';
  }
  
  static public function mkInput($aParams)
  {
    // propriétées communes
    
    if(isset($aParams['id'])){$id = ' id="'.$aParams['id'].'"';}
    else{$id = '';}
    
    if(isset($aParams['class'])){$class = ' class="'.$aParams['class'].'"';}
    else{$class = '';}
    
    if(isset($aParams['name'])){$name = ' name="'.$aParams['name'].'"';}
    else{$name = '';}
    
    if(isset($aParams['script'])){$script = ' '.$aParams['script'];}
    else{$script = '';}
    
    if(isset($aParams['value'])){$value = ' value="'.$aParams['value'].'"';}
    else{$value = '';}
    
    switch($aParams['type'])
    {
      case 'checkbox':
        if(isset($aParams['checked'])){$checked = ' checked="checked"';}
        else{$checked = '';}
        $type=' type="'.$aParams['type'].'"';
        $html = '<input'.$type.$checked.$id.$class.$script.$name.$value.'/>';
        return $html;
    }
  }

  static public function mkList($sName,$aValues,$class="",$type="unordered"){
  $idattrib="";
  if($type=="unordered"){
    $tag="ul";
  }
  else{
    $tag="ol";
  }
  if($sName!=""){
    $idattrib=' id="'.$sName.'"';
  }
  if($class!="")
  {
    $class  = ' class="'.$class.'"';
  }
  $html=EOL.'<'.$tag.$idattrib.$class.'>';
  for($i=0; $i<count($aValues) ; $i++) {
    $html.=EOL.'  <li>'.$aValues[$i].'</li>';
  }
  $html.=EOL.'</'.$tag.'>';
  return $html;
}
  static public function mkTextarea($sName,$sValue="",$sRows="3",$sCols="20",$class="")
  {
    if($class!="")
    {
      $class  = ' class="'.$class.'"';
    }
    $html=EOL.'<textarea '.$class.' name="'.$sName.'" id="'.$sName.'" rows="'.$sRows.'" cols="'.$sCols.'">'.EOL.htmlentities($sValue).EOL.'</textarea>';
    return($html);
  }
  /**
   * Create an html box with title and content divs
   */   
  static public function mkBox($id,$title,$content)
  {
    $html = EOL.'<div class="box" id="'.$id.'">'
           .EOL.' <div class="box-title">'.$title.'</div>'
           .EOL.' <div class="box-content">'
           .EOL.'  '.$content
           .EOL.' </div>'
           .EOL.'</div>';
    return($html);
  }
}
?>
